from __future__ import print_function

import json
import copy


class TrainingMetadata(object):

    def __init__(self):
        self.batch_size = 16
        self.nb_epoch = 1
        self.nb_iter = 1
        self.optimizer = None
        self.shuffle = True
        self.performance = {'loss': dict(), 'train_auc': [], 'val_auc': []}
        self.mode = 'classify'
        self.loss = ''
        self.class_weights = {0:13.458,1:1.4}

    def to_json(self):
        training_metadata = copy.copy(self)
        training_metadata.optimizer = self.optimizer.get_config()
        meta_json = json.dumps(training_metadata, default=lambda o: o.__dict__, indent=4)
        return meta_json

    def save_to_json(self, path):
        # save metadata to json
        f = open(path, 'wb')
        training_json = self.to_json()
        f.write(training_json)
        f.close()

    def update_performance(self, hist_, train_auc, val_auc):
        for k, v in hist_.items():
            if k == 'batch' or k == 'size':
                continue
            if k not in self.performance['loss']:
                self.performance['loss'][k] = []

            self.performance['loss'][k] += hist_[k]
        self.performance['train_auc'] += train_auc
        self.performance['val_auc'] += val_auc
        return self.performance

